{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-10-21T04:13:29.106975Z",
     "start_time": "2025-10-21T04:13:27.584194Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import Perceptron\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "\n",
    "df = pd.read_csv(\"banknotes.csv\")\n",
    "X = df.iloc[:,:4]\n",
    "y = df.iloc[:,4]\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-21T04:13:29.134975Z",
     "start_time": "2025-10-21T04:13:29.121865Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 支持向量机\n",
    "clf = SVC(kernel=\"linear\")\n",
    "clf.fit(X_train,y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "accu = accuracy_score(y_test,y_pred)\n",
    "print(f\"svm 准确率:{accu}\")"
   ],
   "id": "26fcb4aa40553432",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svm 准确率:0.9927272727272727\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-21T04:13:29.152058Z",
     "start_time": "2025-10-21T04:13:29.143248Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 感知机\n",
    "clf = Perceptron()\n",
    "clf.fit(X_train,y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "accu = accuracy_score(y_test,y_pred)\n",
    "print(f\"感知机模型 准确率:{accu}\")"
   ],
   "id": "c4224b55c9ea5e65",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感知机模型 准确率:0.9890909090909091\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-21T04:13:29.170982Z",
     "start_time": "2025-10-21T04:13:29.161492Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# k近邻\n",
    "clf = KNeighborsClassifier(n_neighbors=5)\n",
    "clf.fit(X_train, y_train)\n",
    "y_pred = clf.predict(X_test)\n",
    "accu = accuracy_score(y_test, y_pred)\n",
    "print(f\"k邻近算法 准确率:{accu}\")"
   ],
   "id": "faea0882135dc26f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "k邻近算法 准确率:1.0\n"
     ]
    }
   ],
   "execution_count": 4
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
